/*
  Part of the MasterTool3D project - http://code.google.com/p/master-tool-3d

  Copyright (c) 2011 Ingo Pueschl

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation, version 2.1.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package techjit.gfx.core;

import java.awt.Point;

import processing.core.PApplet;
import techjit.gfx.util.PU;
import techjit.gfx.util.geo.Vector;
import techjit.util.LogManager;

public abstract class GFX_Node extends GFX_SceneElement {

	// FIELDS
	public float screenX515;
	public float screenY515;
	public float screenX505;
	public float screenY505;

	public float modelX505;
	public float modelY505;
	public float modelZ505;

	public float modelX515;
	public float modelY515;
	public float modelZ515;

	public Point[] screenPos = new Point[8];

	// CONSTRUCTORS
	public GFX_Node(GFX_Scene rootScene) {
		// head
		super(rootScene);
	}

	// METHODS
	@Override
	protected void draw(PApplet p) {
		super.draw(p);
		storeScreen(p);
	}

	private Vector getLocalScreenPosition(PApplet p, Vector factor) {
		Vector result;
		p.pushMatrix();
		{
			PU.translate(p, factor);
			result = new Vector(p.screenX(0, 0, 0), p.screenY(0, 0, 0),
					p.screenZ(0, 0, 0));
		}
		p.popMatrix();
		return result;
	}

	public boolean isTopNode() {
		GFX_LineLayout parentLineLayout = (GFX_LineLayout) navi_getParentObject();
		if (parentLineLayout.isTopLayout()) {
			return true;
		}
		return false;
	}

	@Override
	public void log() {
		LogManager.logALL("<GFX_Node>");
		LogManager.indent();
		{
			super.log();
		}
		LogManager.deindent();
	}

	private void storeScreen(PApplet p) {
		Vector model505 = getLocalModelPosition(p, new Vector(.5f, 0, .5f));
		Vector model515 = getLocalModelPosition(p, new Vector(.5f, 1, .5f));

		modelX505 = model505.getX();
		modelY505 = model505.getY();
		modelZ505 = model505.getZ();

		modelX515 = model515.getX();
		modelY515 = model515.getY();
		modelZ515 = model515.getZ();

		// Vector screen505 = new Vector(0);
		// Vector screen515 = new Vector(0);

		Vector screen505 = getLocalScreenPosition(p, new Vector(.5f, 0, .5f));
		Vector screen515 = getLocalScreenPosition(p, new Vector(.5f, 1, .5f));

		screenX505 = screen505.getX();
		screenY505 = screen505.getY();
		screenX515 = screen515.getX();
		screenY515 = screen515.getY();

		Vector v000 = getLocalScreenPosition(p, new Vector(0));
		Vector v001 = getLocalScreenPosition(p, new Vector(0, 0, 1));
		Vector v010 = getLocalScreenPosition(p, new Vector(0, 1, 0));
		Vector v011 = getLocalScreenPosition(p, new Vector(0, 1, 1));
		Vector v100 = getLocalScreenPosition(p, new Vector(1, 0, 0));
		Vector v101 = getLocalScreenPosition(p, new Vector(1, 0, 1));
		Vector v110 = getLocalScreenPosition(p, new Vector(1, 1, 0));
		Vector v111 = getLocalScreenPosition(p, new Vector(1));

		screenPos[0] = new Point((int) v000.getX(), (int) v000.getY());
		screenPos[1] = new Point((int) v001.getX(), (int) v001.getY());
		screenPos[2] = new Point((int) v010.getX(), (int) v010.getY());
		screenPos[3] = new Point((int) v011.getX(), (int) v011.getY());
		screenPos[4] = new Point((int) v100.getX(), (int) v100.getY());
		screenPos[5] = new Point((int) v101.getX(), (int) v101.getY());
		screenPos[6] = new Point((int) v110.getX(), (int) v110.getY());
		screenPos[7] = new Point((int) v111.getX(), (int) v111.getY());

		//		techjit.home.elena.HomeER homeER = new techjit.home.elena.HomeER();
		//
		//		if (homeER.isPointInBox(screenPos, new Point(p.mouseX, p.mouseY))) {
		//			LogManager.debugIP("mouse on " + this + " with z = " + v000.getZ() + ","
		//					+ v111.getZ());
		//		}
	}

}
